package ypt.fqh.maptest1.helper;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import com.tencent.tencentmap.mapsdk.maps.model.Marker;

public class SensorEventHelper implements SensorEventListener {

    private SensorManager mSensorManager;

    private Sensor mSensor;

    private Context mContext;

    private long lastTime = 0;

    private final int TIME_SENSOR = 100;

    private float mAngle;

    private Marker mMarker;

    public SensorEventHelper(Context context) {
        mContext = context;
        mSensorManager = (SensorManager) context
                .getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (System.currentTimeMillis() - lastTime < TIME_SENSOR) {
            return;
        }
        float[] values = event.values;
        float x_1 = values[0];
        float y_1 = values[1];
        float z_1 = values[2];
        System.out.println("sensorType="+event.sensor.getType()+",x="+x_1+",y="+y_1+",z="+z_1);
        if (mMarker != null) {
            System.out.println("设置旋转角度");
            mMarker.setRotation(180);
        }
//        switch (event.sensor.getType()) {
//            case Sensor.TYPE_ORIENTATION: {
//                float x = event.values[0];
//                x += getScreenRotationOnPhone(mContext);
//                x %= 360.0F;
//                if (x > 180.0F)
//                    x -= 360.0F;
//                else if (x < -180.0F)
//                    x += 360.0F;
//
//                if (Math.abs(mAngle - x) < 3.0f) {
//                    break;
//                }
//                mAngle = Float.isNaN(x) ? 0 : x;
//                if (mMarker != null) {
//                    mMarker.setRotation(360 - mAngle);
//                }
//                lastTime = System.currentTimeMillis();
//            }
//        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    /**
     * 获取当前屏幕旋转角度
     *
     * @param context
     * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏
     */
    public static int getScreenRotationOnPhone(Context context) {
        final Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        System.out.println("屏幕旋转角度："+display.getRotation());
        switch (display.getRotation()) {
            case Surface.ROTATION_0:
                return 0;

            case Surface.ROTATION_90:
                return 90;

            case Surface.ROTATION_180:
                return 180;

            case Surface.ROTATION_270:
                return -90;
        }
        return 0;
    }

    public void registerSensorListener() {
        mSensorManager.registerListener(this, mSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void setCurrentMarker(Marker marker) {
        mMarker = marker;
    }

    public void unRegisterSensorListener() {
        mSensorManager.unregisterListener(this, mSensor);
    }
}
